package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("smallVideos")
public class VideoController {
    @Autowired
    private VideoService videoService;
    /**
     * 接口名称：小视频列表
     * 接口路径：GET/smallVideos
     */
    @GetMapping
    public ResponseEntity<Object> queryVideoList(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "1")Integer pagesize) {
        PageResult pageResult = videoService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：视频上传
     * 接口路径：POST/smallVideos
     *
     * MultipartFile
     * 1. springmvc提供的专门用户封装上传的文件的对象
     * 2. 上传的文件默认最大大小是1MB
     * 3. 所以要上传小视频，需要修改yml配置允许上传的文件最大值
     *
     * SpringMVC三大组件：
     * 1. 处理器映射器 RequestMappingHandlerMapper： 根据请求url，找到控制器方法
     * 2. 处理器适配器 RequestMappingHandlerAdapter；找到方法后执行控制器方法
     * 3. 视图解析器  InternalResouceViewResolver； 执行后获处理返回结果
     */
    @PostMapping
    public ResponseEntity<Object> saveVideos(
            MultipartFile videoThumbnail,
            MultipartFile videoFile,String content) throws Exception {
        return videoService.saveVideos(videoThumbnail,videoFile,content);
    }

    /**
     * 接口名称：视频用户关注
     * 接口路径：POST/smallVideos/:uid/userFocus
     * @PathVariable 获取rest请求中路径值
     */
    @PostMapping("{uid}/userFocus")
    public ResponseEntity<Object> userFocus(@PathVariable("uid") Long followUserId){
        return videoService.userFocus(followUserId);
    }

    /**
     * 接口名称：视频用户关注 - 取消
     * 接口路径：POST/smallVideos/:uid/userUnFocus
     */
    @PostMapping("{uid}/userUnFocus")
    public ResponseEntity<Object> userUnFocus(
            @PathVariable("uid") Long followUserId){
        return videoService.userUnFocus(followUserId);
    }
}
